-- ** POPUP SCREENS ** --
-- index
-- [MENU] = IngameMenuScreen
-- [DIAL] = DialogScreen

----------------------
-- Ingame menu screen
-- [MENU]
----------------------
IngameMenuScreen = {}
IngameMenuScreen.__index = IngameMenuScreen

-- constructor
function IngameMenuScreen.new()
    local i = {}
    setmetatable(i, IngameMenuScreen)
    
    -- constants
    i.c_max_item_per_page = 8
    
    -- variables
    i.state = "main"
    i.background = nil
    i.portrait = nil
    i.pointer = nil
    i.pointer_y = 56
    i.pointer_x = 0
    i.menu_id = 0
    i.menu_id_equip = 0
    i.item_count = 0
    i.acquired = nil -- acquired table, acquired stuff
    i.font = nil
    i.title_font = nil
    i.pointer_time = 0.0
    i.offset = -8 -- 0 with love 0.6.2

    -- load content
    i:loadContent()
    
    return i
end

-- load content
function IngameMenuScreen:loadContent()
    -- backgrounds
    self.background = Sprite.new()
    self.background:loadImage("background", "content/gui/ingamemenu-background.png")
    self.background:loadImage("background-2", "content/gui/ingamemenu-background2.png")
    
    -- pointer
    self.pointer = Sprite.new()
    self.pointer:loadImage("pointer", "content/gui/hand-pointer.png")
    
    -- portrait
    self.portrait = Sprite.new()
    self.portrait:loadImage("portrait", "content/gui/hero-portrait.png")
    
    -- fonts
    self.font = love.graphics.newFont("content/fonts/visitor2.ttf", 24)
    self.title_font = love.graphics.newFont("content/fonts/upheavtt.ttf", 20)
    
    self.background:setImage("background")
end

-- update
function IngameMenuScreen:update()
    self.pointer_time = (self.pointer_time + global_delta_time) % (math.pi * 2.0)    
end

-- draw
function IngameMenuScreen:draw()
   -- draw background and portrait
    self.background:setImage("background")
    self.background:draw()
    self.portrait:draw(15, 50)
    
    -- draw magic icons
    if left_binded_magic ~= nil then
        left_binded_magic.icon:setScale(1.0, 1.0)
        left_binded_magic.icon:draw(126, 170)
    end
    if right_binded_magic ~= nil then
        right_binded_magic.icon:setScale(1.0, 1.0)
        right_binded_magic.icon:draw(165, 170)
    end
    if down_binded_magic ~= nil then
        down_binded_magic.icon:setScale(1.0, 1.0)
        down_binded_magic.icon:draw(204, 170)
    end
    
    -- draw skill icons
    if double_jump_acquired then
        
    end
    if wall_jump_acquired then
        
    end
    if dash_acquired then
        
    end
    if power_glove_acquired then
    
    end
    
    -- draw pointer
    if self.state == "main" then
        if self.menu_id < 3 then
            self.pointer:draw(105 + math.cos(self.pointer_time * 5.0) * 2.0, self.pointer_y)
        else
            self.pointer:draw(self.pointer_x + math.cos(self.pointer_time * 5.0) * 2.0, 180)
        end
    end

    -- draw titles
    love.graphics.setFont(self.title_font)
    drawString("VALGARD", 62, 70 + self.offset)
    drawString("STATUS", 68, 234 + self.offset)
    drawString("EQUIPMENT", 310, 70 + self.offset)
    drawString("MAGIC", 320, 294 + self.offset)
    drawString("SKILL", 72, 378 + self.offset)
    
    -- draw stat texts, stats are from game_screens.lua
    love.graphics.setFont(self.font)
    drawString("LV.", 50, 53 + self.offset)
    drawString("HP", 15, 75 + self.offset)
    drawString("MP", 15, 95 + self.offset)
    drawString("ATK", 15, 128 + self.offset)
    drawString("DEF", 15, 142 + self.offset)
    drawString("LUC", 15, 156 + self.offset)
    drawString("GIL", 180, 7 + self.offset)
    
    drawString(25, 76, 53 + self.offset, 20, "right")
    drawString(99 .. "/" .. 99, 76, 75 + self.offset, 20, "right")
    drawString(99 .. "/" .. 99, 76, 95 + self.offset, 20, "right")
    drawString(99, 76, 128 + self.offset, 20, "right")
    drawString(99, 76, 142 + self.offset, 20, "right")
    drawString(99, 76, 156 + self.offset, 20, "right")
    drawString(money, 215, 7 + self.offset, 20, "right")
    
    -- draw current equipments
    if current_weapon ~= nil then
        drawString(current_weapon.name, 145, 56 + self.offset)
    end
    if current_armor ~= nil then
        drawString(current_armor.name, 145, 85 + self.offset)
    end
    if current_accessory ~= nil then
        drawString(current_accessory.name, 145, 114 + self.offset)
    end
    
    -- draw text of equip menu
    if self.state == "equip" then
        -- backgrounds
        self.background:setImage("background-2")
        self.background:draw()
    
        -- draw pointer
        self.pointer:draw(100 + math.cos(self.pointer_time * 5.0) * 2, self.pointer_y)
    
        -- draw title
        local title = "WEAPON"
        if self.menu_id == 1 then title = "ARMOR" end
        if self.menu_id == 2 then title = "ACCESSORY" x_offset = - 2 end
        love.graphics.setFont(self.title_font)
        drawString(title, 155, 36 + self.offset)
        
        -- draw available equips
        -- tables are from from game_screen.lua
        -- weapons, armors and accessories
        love.graphics.setFont(self.font)
        
        local max_items = self.c_max_item_per_page
        local offset = 0
        if self.item_count < max_items then max_items = self.item_count end
        if self.menu_id_equip >= max_items then offset = self.menu_id_equip - max_items + 1 end
        
        for i = 1, max_items do        
            drawString(self.acquired[i + offset].name, 120, 53 + (i-1) * 14 + self.offset)
        end
        
        -- write description title and atk info
        drawString("Description", 30, 184 + self.offset)
        
        -- current active
        local item = self.acquired[self.menu_id_equip + 1]
        
        -- weapons
        if item.type == "weapon" then
            drawString("ATK " .. item.power,
                175, 
                184 + self.offset, 
                100, 
                "right")
        end
        
        -- description
        if item.type ~= "magic" then
            drawString(item.description, 10, 198 + self.offset, 250, "left")
        else
            drawString(item.description, 30, 198 + self.offset, 250, "left")
            --item.icon:setScale(0.5, 0.5)
            item.icon:draw(10, 199)
        end
    end
end

-- update key press
function IngameMenuScreen:updateKeyPress(key)   
    -- main menu actions
    if self.state == "main" then
        -- return from this screen to game
        if key == "return" then
            screen_manager:removeScreen(2) -- this
        end
        
        -- down
        if key == "s" then
            self.menu_id = self.menu_id + 1
            self.menu_id = self.menu_id % 6
        -- up
        elseif key == "w" then
            self.menu_id = self.menu_id - 1
            if self.menu_id < 0 then self.menu_id = 5 end
        end

        -- update position for the pointer
        if self.menu_id < 3 then
            self.pointer_y = 56 + self.menu_id * 29
        else
            self.pointer_x = 110 + (self.menu_id - 3) * 40
        end

        -- press attack (left)
        if key == "left" then
            -- create new acquired table
            self.acquired = {}
        
            -- set state to equip menu
            self.state = "equip"
            -- init pointer to right location
            self.pointer_y = 53
            
            -- check availabe count
            -- tables are from game_screens.lua
            local temp = nil
            if self.menu_id == 0 then temp = weapons end
            if self.menu_id == 1 then temp = armors end
            if self.menu_id == 2 then temp = accessories end
            
            -- magics
            if self.menu_id > 2 then temp = magics end
            
            -- count acquired and insert them to new table
            for i = 1, #temp do
                if temp[1].acquired then
                    table.insert(self.acquired, temp[i])
                end
            end
            
            -- count
            self.item_count = #self.acquired
        end
    -- equip menu actions
    elseif self.state == "equip" then
        -- down
        if key == "s" then
            self.menu_id_equip = self.menu_id_equip + 1
            self.menu_id_equip = self.menu_id_equip % self.item_count
        -- up
        elseif key == "w" then
            self.menu_id_equip = self.menu_id_equip - 1            
            if self.menu_id_equip < 0 then self.menu_id_equip = self.item_count - 1 end
        end
        
        -- when activate
        if key == "left" then
            -- set it to current weapon (game_screen.lua)
            if self.menu_id == 0 then
                current_weapon = self.acquired[self.menu_id_equip + 1]
            elseif self.menu_id == 1 then
                current_armor = self.acquired[self.menu_id_equip + 1]
            elseif self.menu_id == 2 then
                current_accessory = self.acquired[self.menu_id_equip + 1]
            elseif self.menu_id == 3 then
                left_binded_magic = self.acquired[self.menu_id_equip + 1]
            elseif self.menu_id == 4 then
                right_binded_magic = self.acquired[self.menu_id_equip + 1]
            elseif self.menu_id == 5 then
                down_binded_magic = self.acquired[self.menu_id_equip + 1]
            end
            
            -- reset to main
            self:resetPointerToMain()
            return
        end
        
        if key == "down" then
            -- reset to main
            self:resetPointerToMain()
            return
        end
        
        -- update position for the pointer
        local max_items = self.c_max_item_per_page
        if self.item_count < max_items then max_items = self.item_count end
        
        if self.menu_id_equip < max_items then
            self.pointer_y = 53 + self.menu_id_equip * 14
        elseif self.menu_id_equip > max_items then
            self.pointer_y = 53 + (self.c_max_item_per_page - 1) * 14
        end
    end
end

-- reset pointer
function IngameMenuScreen:resetPointerToMain()
    -- reset to main menu
    self.state = "main"
    
    -- update position for the pointer
    if self.menu_id < 3 then
        self.pointer_y = 56 + self.menu_id * 29
    else
        self.pointer_x = 110 + (self.menu_id-3) * 40
    end
    self.menu_id_equip = 0 
end

-- update key release
function IngameMenuScreen:updateKeyRelease(key)
end



----------------------
-- Dialog popup screen
-- [DIAL]
----------------------
DialogPopupScreen = {}
DialogPopupScreen.__index = DialogPopupScreen

previous_line_index = 1
previous_talker = ""

-- contructor
function DialogPopupScreen.new(talker)
	local d = {}
	setmetatable(d, DialogPopupScreen)
	
	-- constants
	d.c_line_time = 2.0
	d.c_interval = 0.05
	d.c_text_x = 40
	d.c_text_y = 182
	d.c_max_rows = 4
	d.c_max_columns = 45
    d.c_target_y = 45
    d.c_start_y = 224
    d.c_textbox_speed = 4
	
	-- variables
    --d.font = love.graphics.newFont("content/fonts/Chalkduster.ttf", 14)
    d.arrow = Sprite.new()
    d.arrow:loadImage("arrow", "content/gui/marker.png")
	d.background = Sprite.new()
	d.background:loadImage("textbox", "content/gui/dialogbox.png")
    d.background:setAlpha(0.0)
	d.x = (global_width - d.background:getWidth()) * 0.5
	d.y = d.c_start_y -- global_height - d.background:getHeight() - 2
	d.text = getText(talker)
	d.elapsed_time = 0.0
	d.arrow_time = 0.0
    d.textbox_time = 0.0
    d.textbox_state = "bring in"
    
    -- portrait
    d.portrait = Sprite.new()
    d.portrait:loadImage("portrait", "content/world/" .. talker .. ".png")
    
    -- for drawing
    d.start_point = 1
    d.end_point = 1
    
    -- check which line will be drawn
    if previous_talker == talker then
        previous_line_index = ((previous_line_index + 1) % (#d.text + 1))
        previous_line_index = clamp(previous_line_index, 1, #d.text)
    else
        previous_line_index = 1
    end
    
    -- Set previous talker
    previous_talker = talker
	
	-- split array and check if multirow is needed
	local tmp_array = split(d.text[previous_line_index], ' ')
	local tmp_str = ""
	d.text_array = {}
	d.use_multi = false
	
	-- parse text
	for i = 1, #tmp_array do
		if string.len(tmp_array[i]) + 1 + string.len(tmp_str) < d.c_max_columns then
			tmp_str = tmp_str .. "" .. tmp_array[i] .. " "
		else
			table.insert(d.text_array, tmp_str)
			tmp_str = tmp_array[i] .. " "
		end
		
		if #tmp_array == i then
			table.insert(d.text_array, tmp_str)
		end
	end
	
	-- if multi
	if #d.text_array > d.c_max_rows then d.use_multi = true end
    d.rows = #d.text_array
    
    -- check end point
    d.end_point = d.rows
    if d.rows > d.c_max_rows then d.end_point = d.c_max_rows end
    
	return d
end

-- load content
function DialogPopupScreen:loadContent()
	-- do nothing
end

-- draw
function DialogPopupScreen:draw()
	love.graphics.setFont(small_font)

    -- draw background
	self.background:draw(self.x, self.y)
    self.portrait:draw((self.x - 2), (self.y - 1))
    
	-- draw arrow
	y_offset = math.sin(self.arrow_time * 3) * 2
    if self.elapsed_time >= self.end_point * self.c_line_time then
        self.arrow:draw(240, 210 + y_offset)
    end

    -- draw dialog text
	local y_offset = 0
	local font_height = love.graphics.getFont():getHeight() * 0.75
	for i = self.start_point, self.end_point do
		if i <= #self.text_array then
		
			-- draw text
            drawTimedString(self.text_array[i], self.c_text_x, 
				self.c_text_y + (y_offset * font_height),
				self.elapsed_time, (i-1) * self.c_line_time, self.c_interval, true)
		end
		
		y_offset = y_offset + 1
	end
end

-- update
function DialogPopupScreen:update()
    -- textbox animation, when bringgin in
    if self.textbox_state == "bring in" then
        local target = self.c_start_y - self.c_target_y
    
        if self.y >= target then
            self.textbox_time = self.textbox_time + global_delta_time
            self.textbox_time = clamp(self.textbox_time, 0.0, 1.0)
        end
        
        -- Set positions and alpha
        self.y = self.c_start_y - (math.pow(self.textbox_time * self.c_textbox_speed, 2) * self.c_target_y)
        self.background:setAlpha(self.textbox_time * self.c_textbox_speed)
        self.portrait:setAlpha(self.textbox_time * self.c_textbox_speed)
        
        if self.y <= target then
            self.y = target
            self.background:setAlpha(1.0)
            self.textbox_state = "draw text"
        end
    end

    -- draw text after textbox is it's in place
    if self.textbox_state == "draw text" then
        -- update text drawing
        if self.elapsed_time < self.end_point * self.c_line_time then
            self.elapsed_time = self.elapsed_time + global_delta_time
        end
        
        if self.elapsed_time > 999 then self.elapsed_time = 999 end
        self.arrow_time = (self.arrow_time + global_delta_time) % (math.pi  * 2)
    end
end

-- update key press
function DialogPopupScreen:updateKeyPress(key)
	if key == "left" then
        -- you can leave a little early, thats why - 1
        if self.elapsed_time >= (self.rows) * self.c_line_time then
            screen_manager:removeScreen(2) -- this
        end
    
        if self.elapsed_time < (self.end_point * self.c_line_time) then
            self.elapsed_time = (self.end_point * self.c_line_time)
        else
            if self.use_multi then
                self.start_point = self.start_point + self.end_point
                self.end_point = self.end_point + self.c_max_rows
                if self.end_point > self.rows then self.end_point = self.rows end
                
                self.elapsed_time = (self.start_point - 1) * self.c_line_time
            end
        end
    end
end

-- update key release
function DialogPopupScreen:updateKeyRelease(key)
end